<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>


<div id="viewtrainingshuttleruncontent">
<h4>View Shuttlerun Stats</h4>


<form>
	
<select class="form-control"  name="season" id="select3">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM trainingshuttlerun";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}

?>
</select>

<?php

if (isset($_POST['season'])) {
  
  $season = $_POST['season'];

?>
<br>
<select class="form-control" name="playerID" id="select2">

<?php

if (isset($_POST['playerID'])) {

	$user_key3 = $_POST['playerID'];

	$sql3 = "SELECT firstname, lastname 
	FROM users
	WHERE user_key = '$user_key3'";

	$result3 = $db->query($sql3);
	$row3 = $result3->fetch_assoc();

	$firstname3=$row3['firstname']; 
    $lastname3=$row3['lastname']; 

	echo "<option value=\"$user_key3\" selected=\"selected\">$firstname3 $lastname3 is selected</option>";


} else {         	
	echo "<option value=\"\" disabled=\"disabled\" selected=\"selected\">Select a player to view shuttlerun stats</option>";
}



  $sql2 = "SELECT users.firstname, users.lastname, users.user_key, teams.season, teams.playerID
  FROM users
  JOIN teams
  ON users.user_key = teams.playerID
  WHERE teams.season = '$season'
  ORDER BY users.firstname";



  $result2=$db->query($sql2);

  while($row2=$result2->fetch_assoc()){	

    $user_key=$row2['user_key']; 
    $firstname=$row2['firstname']; 
    $lastname=$row2['lastname']; 
  
  echo "<option value=\"$user_key\">$firstname $lastname</option>";

}

?>

 </select>

 <?php } ?>


</form>

<br>

<?php

if (isset($_POST['season']) && isset($_POST['playerID'])) {
	$setseason = $_POST['season'];
	$setplayer = $_POST['playerID'];
	
	echo "<h4> Shuttlerun result of $firstname3 $lastname3 </h4>";

?>

<div class="table-responsive"> 
  <table class="table table-hover">
    <thead>
      <tr>
        <th>Date</th>
        <th>Trap</th>
      </tr>
    </thead>
    <tbody>


<?php

$sql4 = "SELECT date, trap 
	FROM trainingshuttlerun
	WHERE playerID = '$setplayer'
	ORDER BY date DESC";

$result4=$db->query($sql4);

while($row4=$result4->fetch_assoc()){	

    $date = $row4['date']; 
    $trap = round($row4['trap'], 2); 

    echo "
    <tr>
        <td>$date</td>
        <td>$trap</td>
    </tr>
    ";

}

?>


    </tbody>
  </table>
  </div>




</div>

<?php } ?>

<script type="text/javascript">
	
$("#select3").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewshuttlerunplayerstats.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#viewtrainingshuttleruncontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtrainingshuttleruncontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#select2").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewshuttlerunplayerstats.php",
        data: postData,
        success: function(data) { 
        	//(postData);
            $("#viewtrainingshuttleruncontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtrainingshuttleruncontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

</script>